/**
 * 产品资料的属性特征维护 - 编辑页面
 * @author shixc
 * @date 2018/01/17
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            hidden: false,
            form: '',
            rules: {
                GWAN001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWANACTI: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions: {
                sys_y_n: app.datas_("sys_y_n"),
                options1: [],
                options2: []
            }
        },
        callObj
    ;

    var Gwai500_1Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai500_1_edit'
            }, win);
        },
        edit: function () {
            app.ajax({
                xurl: 'crm/gwai500/editGwan',
                xdata: vueData.form,
                loading: true,
                okmsg: '属性特征信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwai500').reloadDetailTableCurr();
                        layer.close(layer.index);
                    }
                }
            });
        },
        add: function () {
            app.ajax({
                xurl: 'crm/gwai500/addGwan',
                xdata: vueData.form,
                loading: true,
                okmsg: '属性特征明细信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai500').reloadDetailTableCurr();
                        layer.close(layer.index);
                    }
                }
            });
        },
        _call: function (obj) {
            if (obj.is_add) {
                if(obj.GWAM005 == '0'){
                    vueData.hidden = true;
                }else{
                    vueData.hidden = false;
                }
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        GWANACTI: 'Y',
                        GWAN001: obj.GWAN001,
                        GWAN002: obj.GWAN002,
                        GWAIL003: obj.GWAIL003
                    },
                    reset: true
                });
                app.ajax({
                    xurl: 'crm/gwai500/getGwajList',
                    xdata: {GWAJ001: obj.GWAN002},
                    cb: function (result) {
                        vueData.selectOptions.options2 = result.data.list;
                    }
                });
            } else {
                vueData.isAdd = false;
                callObj = obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                app.ajax({
                    xurl: 'crm/gwai500/getGwajList',
                    xdata: {GWAJ001: obj.data.GWAN002},
                    cb: function (result) {
                        vueData.selectOptions.options2 = result.data.list;
                    }
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['GWAN']);
            var vue = new Vue({
                el: '#gwai500_1_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(layer.index);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.add();
                                } else {
                                    self.edit();
                                }
                            }
                        });
                    },
                    change1: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.options1.find(function (item) {
                            return item.GWAI001 === val
                        });
                        vueData.form.GWAIL003 = obj.GWAIL003;
                    },
                    change2: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.options2.find(function (item) {
                            return item.GWAJ002 === val
                        });
                        vueData.form.GWAJL004 = obj.GWAJL004;
                        vueData.form.GWAJL005 = obj.GWAJL005;
                    }
                },
                created: function () {
                    self._setEleIsReady('gwai500_1_edit');
                }
            });
        },
        _lazy: function () {
            app.ajax({
                xurl: 'crm/gwai500/getGwaiList',
                cb: function (result) {
                    vueData.selectOptions.options1 = result.data.list;
                }
            });
            app.ajax({
                xurl: 'crm/gwai500/getGwajList',
                xdata: {GWAJ001: vueData.form.GWAN002},
                cb: function (result) {
                    vueData.selectOptions.options2 = result.data.list;
                }
            });
            this._super();
        }
    });

    return new Gwai500_1Edit();

});